import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/yt_form/props/brn_text_input_props.dart';

class TextInputController extends GetxController {
  late BrnTextInputProps fieldProps;

  late TextEditingController textEditingController;
  // 按钮 enable
  bool enable = false;

  @override
  void onInit() {
    super.onInit();

    if (Get.arguments != null) {
      fieldProps = Get.arguments;
    }
    if (fieldProps.textEditingController != null) {
      textEditingController = fieldProps.textEditingController!;
    } else {
      textEditingController =
          TextEditingController(text: fieldProps.textString);
    }
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  void onChange(String text) {
    if (text.isNotEmpty) {
      enable = true;
    } else {
      enable = false;
    }
    update();
  }

  void submit() {
    if (textEditingController.text.isNotEmpty) {
      fieldProps.onSubmit?.call(textEditingController.text);
      Get.back(result: textEditingController.text);
    }
  }
}
